home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 436_01 / stringz.asm < prev    next >
Assembly Source File  |  1994-10-07  |  36KB  |  897 lines

  1. ;==========================================================================
  2. ;   STRINGZ.ASM
  3. ;
  4. ; C-callable routines that offer string handling capabilities similar
  5. ; to those found in BASIC, and then some.
  6. ;
  7. ; None of the routines in this file allocate memory for, or check for
  8. ; overflow in copying to, destination strings.  It is the responsibility
  9. ; of the calling routine to insure that enough space is available for
  10. ; the destination strings, including terminating null bytes.
  11. ;
  12. ;    Assembler:     Borland TASM 1.01 (tasm /ml/t/w2/z stringz;)
  13. ;
  14. ; INCON source files and the object and library files created from
  15. ; them are:
  16. ;    Copyright (c) 1993-94, Richard Zigler.
  17. ; You may freely distribute unmodified source, object, and library
  18. ; files, and incorporate them into your own non-commercial software,
  19. ; provided that this paragraph and the copyright string defined in
  20. ; INCON.C are included in all copies.
  21. ;==========================================================================
  22. ; Function Prototypes
  23. ;
  24. ; The string pointer and function return types are all declared
  25. ; "far", so that these routines can be linked with programs under
  26. ; any memory model.  The functions in this source file can handle
  27. ; strings up to 32,767 bytes long.
  28. ;
  29. ; char far * pascal far LeftStr  ( char far *, char far *, short         );
  30. ; char far * pascal far RightStr ( char far *, char far *, short         );
  31. ; char far * pascal far MidStr   ( char far *, char far *, short, short  );
  32. ; char far * pascal far lJust    ( char far *, char far *, short, short  );
  33. ; char far * pascal far rJust    ( char far *, char far *, short, short  );
  34. ; char far * pascal far cJust    ( char far *, char far *, short, short  );
  35. ; char far * pascal far lTrim    ( char far *, char far *                );
  36. ; char far * pascal far rTrim    ( char far *, char far *                );
  37. ; char far * pascal far ReplStr  ( char far *, char far *, short, short  );
  38. ; char far * pascal far InsStr   ( char far *, char far *, short, short  );
  39. ; char far * pascal far TemplStr ( char far *, char far *, short         );
  40. ;==========================================================================
  41.  
  42.          locals                         ;enable TASM local symbols
  43.           model     small, pascal       ;set PASCAL stack handling
  44.  
  45. ;--------------------------------------------------------------------------
  46. ; Macros to push registers at entry and pop registers at exit.
  47. ;--------------------------------------------------------------------------
  48.  
  49.       @PushRegs     macro reglist
  50.            ifnb     <reglist>
  51.             irp     reg, <reglist>
  52.            push     reg
  53.            endm
  54.           endif
  55.                     endm
  56.  
  57.        @PopRegs     macro reglist
  58.            ifnb     <reglist>
  59.             irp     reg, <reglist>
  60.             pop     reg
  61.            endm
  62.           endif
  63.                     endm
  64.  
  65.           .code
  66.  
  67. ;--------------------------------------------------------------------------
  68. ;       LeftStr
  69. ;
  70. ; Copies Num characters from left end of SrcStr to DestStr; returns
  71. ; pointer to DestStr.
  72. ;
  73. ; If Num is greater than or equal to the length of SrcStr, all of
  74. ; SrcStr is copied.  If Num is outside the range 1 to 32767, DestStr
  75. ; is returned null.
  76. ;--------------------------------------------------------------------------
  77.  
  78.          public     LEFTSTR
  79.         LEFTSTR     proc far
  80.  
  81.             arg     Num       :word               ;characters to copy
  82.             arg     SrcStr    :far ptr byte       ;source string
  83.             arg     DestStr   :far ptr byte       ;destination string
  84.  
  85.      ParmSize =     type Num + type SrcStr + type DestStr
  86.  
  87.       @PushRegs     <si, di, ds>
  88.             mov     dx, ParmSize
  89.             les     di, [DestStr]
  90.            call     NullPtr
  91.             mov     dx, es              ;save for return
  92.             mov     bx, di
  93.             mov     ax, [Num]           ;# chars to move
  94.             and     ax, ax
  95.             jle     @@Term
  96.             lds     si, [SrcStr]
  97.            call     StrLen              ;CX = source length
  98.             cmp     cx, ax
  99.             jle     @@Move
  100.             mov     cx, ax
  101. @@Move:
  102.       rep movsb
  103. @@Term:
  104.             sub     al, al
  105.           stosb
  106.             mov     ax, bx              ;DX:AX->DestStr
  107.        @PopRegs     <ds, di, si>
  108.             ret
  109.         LEFTSTR     endp
  110.  
  111. ;--------------------------------------------------------------------------
  112. ;      RightStr
  113. ;
  114. ; Copies Num characters from right end of SrcStr to DestStr; returns
  115. ; pointer to DestStr.
  116. ;
  117. ; If Num is greater than or equal to the length of SrcStr, all of
  118. ; SrcStr is copied.  If Num is outside the range 1 to 32767, DestStr
  119. ; is returned null.
  120. ;--------------------------------------------------------------------------
  121.  
  122.          public     RIGHTSTR
  123.        RIGHTSTR     proc far
  124.  
  125.             arg     Num       :word               ;characters to copy
  126.             arg     SrcStr    :far ptr byte       ;source string
  127.             arg     DestStr   :far ptr byte       ;destination string
  128.  
  129.      ParmSize =     type Num + type SrcStr + type DestStr
  130.  
  131.       @PushRegs     <si, di, ds>
  132.             mov     dx, ParmSize
  133.             les     di, [DestStr]
  134.            call     NullPtr
  135.            push     es                  ;save for return
  136.            push     di
  137.             lds     si, [SrcStr]
  138.            call     StrLen              ;CX = source length
  139.             and     cx, cx
  140.             jle     @@Term              ;if out of range
  141.             mov     bx, cx              ;else save in BX
  142.             mov     dx, cx              ; and DX
  143.             mov     cx, [Num]           ;# chars to copy
  144.             and     cx, cx
  145.             jle     @@Term              ;if Num out of range
  146.             sub     dx, cx              ;else if length > Num
  147.              jg     @@NumChars          ; move Num characters
  148.             mov     cx, bx              ;else move length chrs
  149.             jmp     short @@Move
  150. @@NumChars:
  151.             add     si, dx              ;start at length - Num
  152. @@Move:
  153.       rep movsb
  154. @@Term:
  155.             sub     al, al
  156.           stosb                         ;plant null terminator
  157.             pop     ax
  158.             pop     dx                  ;DX:AX->DestStr
  159.        @PopRegs     <ds, di, si>
  160.             ret
  161.        RIGHTSTR     endp
  162.  
  163. ;--------------------------------------------------------------------------
  164. ;        MidStr
  165. ;
  166. ; Copies Num characters from SrcStr to DestStr beginning at Start in
  167. ; SrcStr; returns pointer to DestStr.
  168. ;
  169. ; If Start is greater than the length of SrcStr, DestStr is returned
  170. ; null.  If Num is greater than the remainder of SrcStr, all characters
  171. ; from Start to the end of SrcStr are copied.  If either Start or Num
  172. ; is outside the range 1 to 32767, DestStr is returned null.
  173. ;--------------------------------------------------------------------------
  174.  
  175.          public     MIDSTR
  176.          MIDSTR     proc far
  177.  
  178.             arg     Num       :word               ;characters to copy
  179.             arg     Start     :word               ;start position
  180.             arg     SrcStr    :far ptr byte       ;source string
  181.             arg     DestStr   :far ptr byte       ;destination string
  182.  
  183.      ParmSize =     type Num + type Start + type SrcStr
  184.      ParmSize =     ParmSize + type DestStr
  185.  
  186.       @PushRegs     <si, di, ds>
  187.             mov     dx, ParmSize
  188.             les     di, [DestStr]
  189.            call     NullPtr
  190.            push     es                  ;save for return
  191.            push     di
  192.             mov     bx, [Start]
  193.             and     bx, bx
  194.             jle     @@Term              ;if Start <= 0
  195.             mov     dx, [Num]
  196.             and     dx, dx
  197.             jle     @@Term              ;if Num <= 0
  198.             lds     si, [SrcStr]        ;else load source
  199.            call     StrLen              ;CX = source length
  200.             and     cx, cx
  201.             jle     @@Term              ;if out of range
  202.             cmp     bx, cx
  203.              jg     @@Term              ;if Start > length
  204.